<template>

  <div class="container">
    <div class="box">
      <div class="box-inner">
        <h1>输入课程号</h1>
        <el-form class="form">
          <el-input size="large" v-model="form.course_id" placeholder="输入课程号" type="text" maxlength="20" />


          <div v-if="!$store.state.user.isMobile">
            <SliderCheck :successFun="handleSuccessFun" :errorFun="handleErrorFun" style="margin-bottom: 20px;" />
          </div>
          <el-button type="primary" :loading="form.loading" @click="handleSubmit" style="width: 30%;" size="large">
            加入
          </el-button>


          <el-button type="primary" @click="handleCancel" style="width: 30%;" size="large">取消</el-button>

        </el-form>

      </div>
    </div>
  </div>




</template>


<script lang="ts">
import { defineComponent, ref } from 'vue'
import SliderCheck from '@/views/main/public/publicKit/slider_check.vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { joinCourseApi } from '@/api/course'
import { ElMessage } from 'element-plus'
export default defineComponent({
  create() {

  },
  components: {
    SliderCheck
  },
  methods: {

  },
  setup() {

    const router = useRouter()
    const store = useStore()
    const form = ref({
      user_id: store.state.user.user_id,
      is_teacher: store.state.user.is_teacher,
      status: false,
      loading: false,
      course_id: '',
    })
    // 滑块验证成功回调
    const handleSuccessFun = () => {
      form.value.status = true
      console.log('验证成功')
    }
    // 滑块验证失败回调
    const handleErrorFun = () => {
      form.value.status = false
      console.log('验证失败')
    }
    // 提交表单
    const handleSubmit = () => {
      if (!store.state.user.isMobile)
        if (!form.value.status) {
          ElMessage.error('请完成滑块验证')
          return
        }
      form.value.loading = true
      joinCourseApi(form.value).then((res: any) => {
        if (res.code === 200) {
          ElMessage.success('加入课程成功')
          router.go(-1) // 跳转到课程页面
        } else {
          ElMessage.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
      setTimeout(() => {
        form.value.loading = false
        form.value.status = true
        console.log('提交成功')
      }, 1000)
    }
    // 取消表单
    const handleCancel = () => {
      form.value.status = false
      console.log('取消成功')
      router.back()
    }
    return {
      handleSuccessFun,
      handleErrorFun,
      handleSubmit,
      handleCancel,
      form,
    }
  }
})
</script>

<style lang="scss" scoped>
.container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background: #fff url('@/assets/login/bg.png') no-repeat center;
  overflow: hidden;
  background-size: cover;
  cursor: pointer;
  user-select: none;

  .box {
    width: 500px;
    display: flex;
    position: absolute;
    left: 50%;
    top: 30%;
    background: white;
    border-radius: 8px;
    transform: translate(-50%, -50%);
    height: 360px;
    overflow: hidden;
    box-shadow: 0 6px 20px 5px rgba(152, 152, 152, 0.1),
      0 16px 24px 2px rgba(117, 117, 117, 0.14);


    .box-inner {
      width: 500px;

      h1 {
        margin-top: 30px;
        text-align: center;
      }

      .form {
        width: 80%;
        margin: 50px auto 15px;

        .el-input {
          margin-bottom: 30px;
        }

        .el-button {
          margin: 5%;
          justify-content: center;
          text-align: center;
          align-content: center;
          border-radius: 2px;
        }
      }


    }
  }
}

@media screen and (max-width: 700px) {

  .container .box,
  .container .box-inner {
    width: 100vw !important;
    height: 100vh;
    box-shadow: none;
    left: 0;
    top: 0;
    transform: none;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    h1 {
      margin-top: 0;
    }

    .form {}
  }
}
</style>